<?php
class Wlib_Mail_Mailer
{
    const TEST_MODE_OFF = 0;
    const TEST_MODE_ONLY_TEST = 1;
    const TEST_MODE_TEST_AND_REAL = 2;

    /**
     * Переменная определяет будут ли высылатьса письма
     *
     * @var bool
     */
    protected $_isRealSend = true;

    /**
     * Режим тестирование
     *
     * @var integer
     */
    protected $_testMode = 0;

    /**
     * Спиcок тестовых электронных адресов
     *
     * @var array
     */
    protected $_testEmails = array();


    public function __construct($options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        } elseif ($options instanceof Zend_Config) {
            $this->setConfig($options);
        }
    }

    public function setConfig(Zend_Config $config)
    {
        return $this->setOptions($config->toArray());
    }

    public function setOptions(array $options)
    {
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function setIsRealSend($isRealSend)
    {
        $this->_isRealSend = $isRealSend;
        return $this;
    }

    public function getIsRealSend()
    {
        return $this->_isRealSend;
    }

    public function setTestMode($testMode)
    {
        $this->_testMode = $testMode;
        return $this;
    }

    public function getTestMode()
    {
        return $this->_testMode;
    }

    public function setTestEmails($testEmails)
    {
        $this->_testEmails = $testEmails;
        return $this;
    }

    public function getTestEmails()
    {
        return $this->_testEmails;
    }

    public function send($toEmail, $toName, $subject, $htmlText, $fromEmail, $fromName)
    {
        if(!$this->getIsRealSend()) {
            return;
        }

        $testMode = $this->getTestMode();

        $mail = new Zend_Mail('utf-8');
        $mail->setBodyText(strip_tags($htmlText));
        $mail->setBodyHtml($htmlText);
        $mail->setFrom($fromEmail, $fromName);
        $mail->setSubject($subject);

        if($testMode == self::TEST_MODE_OFF || $testMode == self::TEST_MODE_TEST_AND_REAL) {
            $mail->addTo($toEmail, $toName);
            $mail->send();
        }

        if($testMode == self::TEST_MODE_ONLY_TEST || $testMode == self::TEST_MODE_TEST_AND_REAL) {
            $testEmails = $this->getTestEmails();
            if(sizeof($testEmails)) {
                $htmlText = "<b>[This is a test message with is (or should be) sent to '$toName' &lt;$toEmail&gt;]"
                          . "</b><br>\n"
                          . $htmlText;

                $mail->setBodyHtml($htmlText);
                $mail->setBodyText(strip_tags($htmlText));

                $mail->clearRecipients();
                foreach ($testEmails as $email) {
                    $mail->addTo($email);
                }

                $mail->send();
            }
        }
    }
}